const StudentModel = require('../db/models/student');

class StudentServer {
  async addStudentData (data) {
    //检查cid是否存在 check cid is existed
    const sid = data.sid;
    const result = await StudentModel.findOne({where: { sid }});
    //判断 check
    if (result) {
      //更新 update
      return await StudentModel.update(data, {where: { sid }})
    }else {
      //创建 create
      return await StudentModel.create(data);
    }
  }

  async getStudentData () {
    const data =  await StudentModel.findAll({
      attributes: {
        exclude: ['studentImgUrl', 'createdAt', 'updatedAt']
      }  
    });
    return data;
  }

  async changeStudentStatus (id, status) {
     //公用中间件控制器
     const result =  await StudentModel.update({ status }, {
      where: { id }
    })
    return result[0];
  }
}

module.exports = new StudentServer();